import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class vin {
    private String vin;
    private Map VIN_Map = new HashMap<String,String>();
	private Map DICT_VIS_DATE = new HashMap<String,String>();
	
    public vin(String vin,String path) throws IOException {
        this.vin= vin;
		DICT_VIS_DATE.put("K", "1989");
        DICT_VIS_DATE.put("L", "1990");
        DICT_VIS_DATE.put("M", "1991");
        DICT_VIS_DATE.put("N", "1992");
        DICT_VIS_DATE.put("P", "1993");
        DICT_VIS_DATE.put("R", "1994");
        DICT_VIS_DATE.put("S", "1995");
        DICT_VIS_DATE.put("T", "1996");
        DICT_VIS_DATE.put("V", "1997");
        DICT_VIS_DATE.put("W", "1998");
        DICT_VIS_DATE.put("X", "1999");
        DICT_VIS_DATE.put("Y", "2000");
        DICT_VIS_DATE.put("1", "2001");
        DICT_VIS_DATE.put("2", "2002");
        DICT_VIS_DATE.put("3", "2003");
        DICT_VIS_DATE.put("4", "2004");
        DICT_VIS_DATE.put("5", "2005");
        DICT_VIS_DATE.put("6", "2006");
        DICT_VIS_DATE.put("7", "2007");
        DICT_VIS_DATE.put("8", "2008");
        DICT_VIS_DATE.put("9", "2009");
        DICT_VIS_DATE.put("A", "2010");
        DICT_VIS_DATE.put("B", "2011");
        DICT_VIS_DATE.put("C", "2012");
        DICT_VIS_DATE.put("D", "2013");
        DICT_VIS_DATE.put("E", "2014");
        DICT_VIS_DATE.put("F", "2015");
        DICT_VIS_DATE.put("G", "2016");
        DICT_VIS_DATE.put("H", "2017");
        DICT_VIS_DATE.put("J", "2018");
		VIN_Map = readFileByLines(path, VIN_Map);



    }
	 public Map<String, String>  resolve(){
        Map<String, String> result = new HashMap<String, String>();

        String WMI = "";
        String VDS_TYPE = "";
        String key = new String();
        String ffid = new String();


        if (vin == "" || vin == null) {
			result.put("fault","vin为空");

        } else if(vin.length()==17){
            vin = vin.toUpperCase();
            Pattern pattern = Pattern.compile("(L[0-9ABCDEFGHJKLMNPRSTUVWXYZ]{16})");
            Matcher matcher = pattern.matcher(vin);
            if (matcher.find()) {
                ffid = matcher.group(1);
                WMI = ffid.substring(0, 3);
                VDS_TYPE = ffid.substring(0,8);
				key = ffid.substring(9, 10);
				
				if(VIN_Map.containsKey(VDS_TYPE)){
					
					
					
					

					if (DICT_VIS_DATE.containsKey(key)) {
						result.put("year",DICT_VIS_DATE.get(key).toString());
					} else {
						result.put("fault","vin码错误");


					}
					result.put("brand", VIN_Map.get(VDS_TYPE).toString().split(",")[1]);
					result.put("factory",VIN_Map.get(VDS_TYPE).toString().split(",")[0]);
					result.put("type",VIN_Map.get(VDS_TYPE).toString().split(",")[2]);
					
				}else {
					result.put("fault", "国产车不能识别");


				}

				
				
				
				
                
            } else {
				result.put("fault","国外车");

            }

        } else {
			result.put("fault","vin码错误");


        }
        return result;
    }
	private static Map<String, String> readFileByLines(String fileName,Map<String, String> map) throws IOException {
    	File file = new File(fileName);

		 BufferedReader reader = null;

		try {



			 reader = new BufferedReader(new FileReader(file));

			 String tempString = null;

		 int line = 1;

		 // 一次读入一行，直到读入null为文件结束

			         while ((tempString = reader.readLine()) != null) {

			                // 显示行号
						map.put(tempString.split(":")[0], tempString.split(":")[1]) ;



						line++;

			          }

		          reader.close();

			       } catch (IOException e) {

			          e.printStackTrace();

			       } finally {

		            if (reader != null) {

				                 try {

					                    reader.close();

				                } catch (IOException e1) {

					                 }

				             }

			         }
			         return map;

		     }

	public static void main(String[] args) throws IOException {
    	//vin vi = new vin("LFV2A21K9BA234567","D:\\vin_jupyter_notebook\\vin.txt");
    	//System.out.println(vi.resolve().get("brand"));
        if(checkVIN("LBETLBFC4GY153586")){
            System.out.println("正确");

        }
	}



    public  static boolean checkVIN(String vin) {
        Map<Integer, Integer> vinMapWeighting = null;
        Map<Character, Integer> vinMapValue = null;
        vinMapWeighting = new HashMap<Integer, Integer>();
        vinMapValue = new HashMap<Character, Integer>();
        vinMapWeighting.put(1, 8);
        vinMapWeighting.put(2, 7);
        vinMapWeighting.put(3, 6);
        vinMapWeighting.put(4, 5);
        vinMapWeighting.put(5, 4);
        vinMapWeighting.put(6, 3);
        vinMapWeighting.put(7, 2);
        vinMapWeighting.put(8, 10);
        vinMapWeighting.put(9, 0);
        vinMapWeighting.put(10, 9);
        vinMapWeighting.put(11, 8);
        vinMapWeighting.put(12, 7);
        vinMapWeighting.put(13, 6);
        vinMapWeighting.put(14, 5);
        vinMapWeighting.put(15, 4);
        vinMapWeighting.put(16, 3);
        vinMapWeighting.put(17, 2);

        vinMapValue.put('0', 0);
        vinMapValue.put('1', 1);
        vinMapValue.put('2', 2);
        vinMapValue.put('3', 3);
        vinMapValue.put('4', 4);
        vinMapValue.put('5', 5);
        vinMapValue.put('6', 6);
        vinMapValue.put('7', 7);
        vinMapValue.put('8', 8);
        vinMapValue.put('9', 9);
        vinMapValue.put('A', 1);
        vinMapValue.put('B', 2);
        vinMapValue.put('C', 3);
        vinMapValue.put('D', 4);
        vinMapValue.put('E', 5);
        vinMapValue.put('F', 6);
        vinMapValue.put('G', 7);
        vinMapValue.put('H', 8);
        vinMapValue.put('J', 1);
        vinMapValue.put('K', 2);
        vinMapValue.put('M', 4);
        vinMapValue.put('L', 3);
        vinMapValue.put('N', 5);
        vinMapValue.put('P', 7);
        vinMapValue.put('R', 9);
        vinMapValue.put('S', 2);
        vinMapValue.put('T', 3);
        vinMapValue.put('U', 4);
        vinMapValue.put('V', 5);
        vinMapValue.put('W', 6);
        vinMapValue.put('X', 7);
        vinMapValue.put('Y', 8);
        vinMapValue.put('Z', 9);
        boolean reultFlag = false;
        String uppervin = vin.toUpperCase();
        //排除字母O、I
        if (vin == null || uppervin.indexOf("O") >= 0|| uppervin.indexOf("I") >= 0) {
            reultFlag = false;
        } else {
            //1:长度为17
            if (vin.length() == 17) {
                char[] vinArr = uppervin.toCharArray();
                int amount = 0;
                for (int i = 0; i < vinArr.length; i++) {
                    //VIN码从从第一位开始，码数字的对应值×该位的加权值，计算全部17位的乘积值相加
                    amount += vinMapValue.get(vinArr[i])*vinMapWeighting.get(i + 1);
                }
                //乘积值相加除以11、若余数为10，即为字母Ｘ
                if (amount % 11 == 10) {
                    if (vinArr[8] == 'X') {
                        reultFlag = true;
                    } else {
                        reultFlag = false;
                    }

                } else {
                    //VIN码从从第一位开始，码数字的对应值×该位的加权值，

                    if (amount % 11 != vinMapValue.get(vinArr[8])) {
                        reultFlag = false;
                    } else {
                        reultFlag = true;
                    }
                }
            }
            //1:长度不为17
            if (!vin.equals("") && vin.length() != 17) {
                reultFlag = false;
            }
        }
        return reultFlag;
    }

	


}
